<?php

namespace App\Console\Commands;

use App\Models\DomainList;
use App\Models\Setting as SettingModel;
use App\Notifications\CommonMessage;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Notification;

class DomainChange extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'domain:change';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '域名更换任务';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        //获取当前正在使用的域名，把排序降到最低

        $domain_change_switch = SettingModel::query()->firstOrCreate(['key' => 'domain_change_switch'], ['value' => null]);
        if (json_decode($domain_change_switch->value)) {
            //开始更换域名
            $pre_use = json_decode(SettingModel::query()->firstOrCreate(['key' => 'pre_use'], ['value' => null])->value);
            $last_sort = DomainList::query()->orderByDesc('sort')->first()->sort;
            DomainList::query()->orderBy('sort')->limit($pre_use)->update(['sort' => $last_sort + 1]);
        }
    }
}
